<?php
namespace app\api\controller;

class Zabbix extends Lib
{
 
    public function zbx_version()
    {
        $params = [
        ];
        return self::RPC('apiinfo.version', $params);
    }
    
    public function zbx_alert()
    {
        $params = [
            "output"     => "extend",
            "actionids"  => "3"
        ];
        return self::RPC('alert.get', $params);
    }
    
    public function zbx_host_get()
    {
        $params = [
            "output" => ["hostid", "host"],
            "selectInterfaces" => ["interfaceid", "ip" ]
        ];
        return self::RPC('host.get', $params);
    }
    
    public function zbx_application_get()
    {
        $params = [
            "output"      => "extend",
            "selectItems" => ['key_', "name"],
            "sortfield"   => "name",
            "filter"      => [
                "name"    => "CPU"
            ]
        ];
        return self::RPC("application.get", $params);
    }

    public function zbx_item_get($hostid, $grpid, $key)
    {
        $keys = $this->_all_key($key, $hostid, $grpid);
        
        $params = [
            "output"   => "extend",
            //"hostids"  => $hostid,
            //"groupids" => $grpid,
            "filter"   => [
                "key_"  => $keys    
            ],
            // "search"  => [
            //     "key_" => $keys
            // ],
            "sortfield" => "name"
        ];
        //return $keys;
        return self::RPC("item.get", $params);
        // $set = self::RPC("item.get", $params, null, true);
        // $rs = [];
        // foreach($set as $item)
        // {
        //     $rs[] = $item->key_;
        // } 
        // return json_encode($rs); 
    }

    public function zbx_item_enum()
    {
        $params = [
            "output"  => "extend",
            "sortfield" => "name"
        ];
        return self::RPC("item.get", $params);
    }

    public function zbx_key($app_name, $getkey = false)
    {
        $params = [
            "output"      => "extend",
            "selectItems" => ['key_', "name"],
            "sortfield"   => "name",
            "filter"      => [
                "name"    => $app_name
            ]
        ];
        $set = self::RPC("application.get", $params, null, true);
        $rs = [];
        if(!$getkey)
        {
            foreach($set as $item)
            {
                if(!empty($item->items) && count($item->items) > 0)
                {
                    $rs[] = $item;
                }
            }
        }else {
           
            foreach($set as $item)
            {
                $tt = [];
                if(count($item->items) > 0)
                {
                    $tt['applicationid'] = $item->applicationid;
                    $tt['hostid']        = $item->hostid;
                    $tt['name']          = $item->name;
                    $tt['key_']          = [];
                    foreach($item->items as $sitem)
                    {
                        $tt['key_'][] = $sitem->key_;    
                    }
                    $rs[] = $tt;
                }
            } 
        }
        return json_encode($rs);
    }

    public function zbx_all_key($app_name, $hostid = null)
    {
        $rs = $this->_all_key($app_name, $hostid);
        
        return json_encode($rs);
    }
    
    private function _all_key($app_name, $hostid = null, $grpid = null)
    {
        $params = [
            "output"      => "extend",
            "selectItems" => ['key_', "name"],
            "sortfield"   => "name",
            "templated"   => false,
            "filter"      => [
                "name"    => $app_name
            ]
        ];
        if(!empty($hostid))
        {
            $params['filter']['hostid'] = $hostid;
        }
        // return self::RPC("application.get", $params, null, true);
        $set = self::RPC("application.get", $params, null, true);
        $rs = [];
	return $set;
        foreach($set as $item)
        {
            if(!empty($item->items) && count($item->items) > 0)
            {
                foreach($item->items as $sitem)
                {
                    $rs[] = $sitem->key_;    
                }
            }
        } 
        return $rs;
    }
    
    public function zbx_create_action($data)
    {
        $params = $data;
        return self::RPC("action.create", $params);
    }
    
}
